home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / communication / example / ShowDocument.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  4.1 KB  |  131 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.*;
  18. import java.awt.*;
  19. import java.net.URL;
  20. import java.net.MalformedURLException;
  21.  
  22. public class ShowDocument extends Applet {
  23.     URLWindow urlWindow;
  24.  
  25.     public void init() {
  26.         Button button = new Button("Bring up URL window");
  27.         add(button);
  28.         validate();
  29.  
  30.         urlWindow = new URLWindow(getAppletContext());
  31.         urlWindow.pack();
  32.     }
  33.  
  34.     public void destroy() {
  35.         urlWindow.hide();
  36.         urlWindow = null;
  37.     }
  38.  
  39.     public boolean action(Event event, Object o) {
  40.         urlWindow.show();
  41.         return true;
  42.     }
  43. }
  44.  
  45. class URLWindow extends Frame {
  46.     TextField urlField;
  47.     Choice choice;
  48.     AppletContext appletContext;
  49.  
  50.     public URLWindow(AppletContext appletContext) {
  51.         super("Show a Document!");
  52.  
  53.         this.appletContext = appletContext;
  54.  
  55.         GridBagLayout gridBag = new GridBagLayout();
  56.         GridBagConstraints c = new GridBagConstraints();
  57.         setLayout(gridBag);
  58.  
  59.         Label label1 = new Label("URL of document to show:", Label.RIGHT);
  60.         gridBag.setConstraints(label1, c);
  61.         add(label1);
  62.  
  63.         urlField = new TextField("http://java.sun.com/", 40);
  64.         c.gridwidth = GridBagConstraints.REMAINDER;
  65.         c.fill = GridBagConstraints.HORIZONTAL;
  66.         c.weightx = 1.0;
  67.         gridBag.setConstraints(urlField, c);
  68.         add(urlField);
  69.  
  70.         Label label2 = new Label("Window/frame to show it in:", Label.RIGHT);
  71.         c.gridwidth = 1;
  72.         c.weightx = 0.0;
  73.         gridBag.setConstraints(label2, c);
  74.         add(label2);
  75.  
  76.         choice = new Choice();
  77.         choice.addItem("(browser's choice)"); //don't specify
  78.         choice.addItem("My Personal Window"); //a window named "My Personal Window"
  79.         choice.addItem("_blank"); //a new, unnamed window
  80.         choice.addItem("_self"); 
  81.         choice.addItem("_parent"); 
  82.         choice.addItem("_top"); //the Frame that contained this applet
  83.         c.fill = GridBagConstraints.NONE;
  84.         c.gridwidth = GridBagConstraints.REMAINDER;
  85.         c.anchor = GridBagConstraints.WEST;
  86.         gridBag.setConstraints(choice, c);
  87.         add(choice);
  88.  
  89.         Button button = new Button("Show document");
  90.         c.weighty = 1.0;
  91.         c.ipadx = 10;
  92.         c.ipady = 10;
  93.         c.insets = new Insets(5,0,0,0);
  94.         c.anchor = GridBagConstraints.SOUTH;
  95.         gridBag.setConstraints(button, c);
  96.         add(button);
  97.     }
  98.  
  99.     public boolean handleEvent(Event event) {
  100.         if (event.id == Event.WINDOW_DESTROY) {
  101.             hide();
  102.             return true;
  103.         }
  104.         return super.handleEvent(event);
  105.     } 
  106.  
  107.     public boolean action(Event event, Object o) {
  108.         if ((event.target instanceof Button) |
  109.             (event.target instanceof TextField)) {
  110.             String urlString = urlField.getText();
  111.             URL url = null;
  112.             try {
  113.                 url = new URL(urlString);
  114.             } catch (MalformedURLException e) {
  115.                 System.out.println("Malformed URL: " + urlString);
  116.                 return true;
  117.             }
  118.  
  119.             if (url != null) {
  120.                 if (choice.getSelectedIndex() == 0) {
  121.                     appletContext.showDocument(url);
  122.                 } else {
  123.                     appletContext.showDocument(url, choice.getSelectedItem());
  124.                 }
  125.             }
  126.         }
  127.         return true;
  128.     }
  129. }
  130.  
  131.